package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.StoreDTO;
import com.carkeeper.entity.AccountsPO;
import com.carkeeper.entity.BillPO;
import com.carkeeper.mapper.AccountsMapper;
import com.carkeeper.service.AccountsService;
import com.carkeeper.service.BillService;
import com.carkeeper.service.StoreFeign;
import com.carkeeper.vo.AccountsVO;
import com.carkeeper.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;

@Service
public class AccountsServiceImpl implements AccountsService {
    @Autowired
    private AccountsMapper accountsMapper;
    @Autowired
    private BillService billService;
    @Autowired
    private StoreFeign storeFeign;
    /*
    * 新增结算单
    * */
    @Override
    public void addAccounts(AccountsPO accountsPO) {
        BillPO bill = billService.getByStoreId(accountsPO.getStoreId());
        accountsPO.setCreateTime(LocalDateTime.now());
        accountsPO.setMoney(bill.getPrice());
        accountsPO.setUpdateTime(LocalDateTime.now());
        accountsPO.setIsDelete(0);
        accountsMapper.insert(accountsPO);
    }

    /*
    * 获取结算单分页
    * */

    @Override
    public Pager<AccountsPO> getAccountPage(Page<AccountsPO> page) {
        accountsMapper.selectPage(page,null);
        Pager<AccountsPO> pager = new Pager<>(page);
        for (AccountsPO item : pager.getItems()) {
            StoreDTO storeDTO = storeFeign.queryStoreById(item.getStoreId());
            item.setStoreDTO(storeDTO);
        }
        return pager;
    }
    /*
    * 获取结算单详情
    * */
    @Override
    public AccountsPO getAccounts(Long id) {
        AccountsPO accountsPO = accountsMapper.selectById(id);
        StoreDTO storeDTO = storeFeign.queryStoreById(accountsPO.getStoreId());
        accountsPO.setStoreDTO(storeDTO);
        return accountsPO;
    }

    /*
    * 删除结算单
    * */
    @Override
    public void del(Long id) {
        accountsMapper.deleteById(id);
    }
}
